package control;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import service.MessageService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import domain.Message;
import domain.User;

public class MessageAction extends ActionSupport{
	private User user;
	private Message mess;
	private String messId;
	private MessageService messageService;
	private String recName;
	
	public MessageService getMessageService() {
		return messageService;
	}
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	public String getMessId() {
		return messId;
	}
	public void setMessId(String messId) {
		this.messId = messId;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Message getMess() {
		return mess;
	}
	public void setMess(Message mess) {
		this.mess = mess;
	}
	
	public String insertMessage(){
		String MessageContent = mess.getContent();
		String recId = getMessId();
		Map session = ActionContext.getContext().getSession();
		String acc = (String) session.get("acc");
		if(recId==null){
			System.out.println(recName);
			messageService.insertMessage(MessageContent,recName, acc,true);
			return "recSuccess";
		}else{
		messageService.insertMessage(MessageContent,recId, acc,false);
		return ActionSupport.SUCCESS;
		}
		
	}
	
	public String showSendMessage(){
		Map session = ActionContext.getContext().getSession();
		String acc = (String) session.get("acc");
		List<Message> sendMessages = messageService.showSendMessage(acc);
		Map request = (Map)	ActionContext.getContext().get("request");
		request.put("messages", sendMessages);
		return SUCCESS;
	}
	
	public String showRecMessage(){
		Map session = ActionContext.getContext().getSession();
		String acc = (String) session.get("acc");
		List<Message> recMessages = messageService.showRecMessage(acc);
		Map request = (Map)	ActionContext.getContext().get("request");
		request.put("messages", recMessages);
		return SUCCESS;
	}
	
	public String deleteMessage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] mIds = request.getParameterValues("mId");
		messageService.deleteMessage(mIds);
		return SUCCESS;
	}
	public String getRecName() {
		return recName;
	}
	public void setRecName(String recName) {
		this.recName = recName;
	}
	
}
